package com.yeyuanqi.hottop.controller;

import com.yeyuanqi.hottop.common.Result;
import com.yeyuanqi.hottop.service.*;
import com.yeyuanqi.hottop.untils.CalendarPieDataUtil;
import com.yeyuanqi.hottop.untils.PieChartDataUtil;
import com.yeyuanqi.hottop.vo.HotListVO;
import com.yeyuanqi.hottop.vo.PieChartVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 饼图控制器
 * @author yeyuanqi
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/piechart")
public class PieChartController {

    @Autowired
    private CalendarPieDataUtil calendarPieDataUtil;

    @Autowired
    private PieChartDataUtil pieChartDataUtil;

    @Autowired
    private RedisService redisService;

    @Autowired
    private TBaiduhotService tBaiduhotService;

    @Autowired
    private TJinrihotService tJinrihotService;

    @Autowired
    private TTxhotService tTxhotService;

    @Autowired
    private TWeibohotService tWeibohotService;

    /**
     * 该月全部平台每天热搜的top1
     * @return 日历饼图数据
     */
    @GetMapping("/calendarpie")
    public Result getCalendarPieData(){
        return Result.succ(calendarPieDataUtil.getMonthlyTopOneData());
    }

    /**
     * 某月全部平台每天热搜的top1
     * @return 日历饼图数据
     */
    @GetMapping("/calendarpie/{yearMonth}")
    public Result getCalendarPieDataByMonth(@PathVariable("yearMonth") String yearMonth){
        return Result.succ(calendarPieDataUtil.getTopOneDataByMonth(yearMonth));
    }
    /**
     * 某平台的最新热搜数据
     * @return 可滚动的图例饼图数据
     * [{name : String,value: Integer},{....}.....]
     */
    @GetMapping("/piewithscrollablelegend/{sitename}")
    public Result getPieWithScrollableLegendData(@PathVariable("sitename") String sitename){
        switch (sitename){
            case "bdnewhot":
                //查询缓存，有缓存直接返回
                if (redisService.lSize("bdPieData")!=0){
                    List<Object> bdtopnewhotList= redisService.lRange("bdPieData", 0, -1);
                    return Result.succ(bdtopnewhotList);
                }
                List<HotListVO> bdhotListVOS = tBaiduhotService.selectNewHot();
                PieChartVO bdpieHotData = pieChartDataUtil.getPieHotData(bdhotListVOS);
                List<PieChartVO> bdhotList = new ArrayList<>();
                bdhotList.add(bdpieHotData);
                return Result.succ(bdhotList);
            case "jrnewhot":
                if (redisService.lSize("jrPieData")!=0){
                    List<Object> jrtopnewhotList= redisService.lRange("jrPieData", 0, -1);
                    return Result.succ(jrtopnewhotList);
                }
                List<HotListVO> jrhotListVOS = tJinrihotService.selectNewHot();
                PieChartVO jrpieHotData = pieChartDataUtil.getPieHotData(jrhotListVOS);
                List<PieChartVO> jrhotList = new ArrayList<>();
                jrhotList.add(jrpieHotData);
                return Result.succ(jrhotList);
            case "txnewhot":
                if (redisService.lSize("txPieData")!=0){
                    List<Object>  txtopnewhotList= redisService.lRange("txPieData", 0, -1);
                    return Result.succ(txtopnewhotList);
                }
                List<HotListVO> txhotListVOS = tTxhotService.selectNewHot();
                PieChartVO txpieHotData = pieChartDataUtil.getPieHotData(txhotListVOS);
                List<PieChartVO> txhotList = new ArrayList<>();
                txhotList.add(txpieHotData);
                return Result.succ(txhotList);
            case "wbnewhot":
                if (redisService.lSize("wbLineAndBarData")!=0){
                    List<Object>  wbtopnewhotList= redisService.lRange("wbPieData", 0, -1);
                    return Result.succ(wbtopnewhotList);
                }
                List<HotListVO> wbhotListVOS = tWeibohotService.selectNewHot();
                PieChartVO wbpieHotData = pieChartDataUtil.getPieHotData(wbhotListVOS);
                List<PieChartVO> wbhotList = new ArrayList<>();
                wbhotList.add(wbpieHotData);
                return Result.succ(wbhotList);
            default:
                return Result.fail("没有该网站的热搜信息");
        }
    }

    /**
     *  历史热搜数据
     *  @return 可滚动的图例饼图数据
     */
    @GetMapping("/piewithscrollablelegend/{sitename}/{day}/{min}")
    public Result selectLineChartForNewHotByTime(@PathVariable("sitename") String sitename , @PathVariable("day") String day, @PathVariable("min") String min){
        if ("all".equals(min)) {
            min = "%";
        }else if (!"".equals(min) && null != min){
            min = min + "%";
        }
        switch (sitename){
            case "bdhot":
                List<HotListVO> bdhotListVOS = tBaiduhotService.selectHotByTime(day, min);
                if (bdhotListVOS.size() != 0){
                    PieChartVO bdnewshotList = pieChartDataUtil.getPieHotData(bdhotListVOS);
                    List<PieChartVO> bdhotList = new ArrayList<>();
                    bdhotList.add(bdnewshotList);
                    return Result.succ(bdhotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            case "jrhot":
                List<HotListVO> jrhotListVOS = tJinrihotService.selectHotByTime(day, min);
                if (jrhotListVOS.size() != 0){
                    PieChartVO jrnewshotList = pieChartDataUtil.getPieHotData(jrhotListVOS);
                    List<PieChartVO> jrhotList = new ArrayList<>();
                    jrhotList.add(jrnewshotList);
                    return Result.succ(jrhotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            case "txhot":
                List<HotListVO> txhotListVOS = tTxhotService.selectHotByTime(day, min);
                if (txhotListVOS.size() != 0){
                    PieChartVO txnewshotList = pieChartDataUtil.getPieHotData(txhotListVOS);
                    List<PieChartVO> txhotList = new ArrayList<>();
                    txhotList.add(txnewshotList);
                    return Result.succ(txhotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            case "wbhot":
                List<HotListVO> wbhotListVOS = tWeibohotService.selectHotByTime(day, min);
                if (wbhotListVOS.size() != 0){
                    PieChartVO wbnewshotList = pieChartDataUtil.getPieHotData(wbhotListVOS);
                    List<PieChartVO> wbhotList = new ArrayList<>();
                    wbhotList.add(wbnewshotList);
                    return Result.succ(wbhotList);
                } else {
                    return Result.fail("没有该时间的热搜");
                }
            default:
                return Result.fail("没有该网站的热搜信息");
        }
    }

}
